<template>
  <div class="blog-detail-container">
    <div class="blog-detail-wrapper">

      <div class="blog-main">
        <!-- 博客内容卡片 -->
        <div class="blog-content-card">
          <div class="blog-header">
            <h1 class="blog-title">{{ blog.title }}</h1>
            <div class="blog-meta">
              <div class="meta-item">
                <i class="el-icon-user"></i>
                <span>{{ blog.userName }}</span>
              </div>
              <div class="meta-item">
                <i class="el-icon-date"></i>
                <span>{{ blog.date }}</span>
              </div>
              <div class="meta-item">
                <i class="el-icon-view"></i>
                <span>{{ blog.readCount }}</span>
              </div>
            </div>
            <div class="blog-tags" v-if="tagsArr && tagsArr.length">
              <el-tag v-for="item in tagsArr" :key="item" class="blog-tag">{{ item }}</el-tag>
            </div>
          </div>

          <div class="blog-content">
            <div class="w-e-text" v-html="blog.content"></div>
          </div>
        </div>

        <!-- 互动操作卡片 -->
        <div class="interaction-card">
          <div class="interaction-item" @click="setLikes" :class="{ 'active' : blog.userLike }">
            <i class="el-icon-thumb"></i>
            <span>{{ blog.likesCount }}</span>
            <div class="interaction-label">点赞</div>
          </div>
          <div class="interaction-item" @click="setCollect" :class="{ 'active' : blog.userCollect }">
            <i class="el-icon-star-off"></i>
            <span>{{ blog.collectCount }}</span>
            <div class="interaction-label">收藏</div>
          </div>
        </div>

        <!-- 评论区域 -->
        <div class="comment-section">
          <Comment :fid="blogId" module="博客"/>
        </div>
      </div>

      <!-- 侧边栏 -->
      <div class="blog-sidebar">
        <!-- 作者信息卡片 -->
        <div class="author-card">
          <div class="author-info">
            <img :src="blog.user?.avatar" alt="" class="author-avatar">
            <div class="author-details">
              <div class="author-name">{{ blog.user?.name }}</div>
              <div class="author-bio">{{ blog.user?.info }}</div>
            </div>
          </div>

          <div class="author-stats">
            <div class="stat-item">
              <div class="stat-number">{{blog.user?.blogCount}}</div>
              <div class="stat-label">文章</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{blog.user?.likesCount}}</div>
              <div class="stat-label">点赞</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{blog.user?.collectCount}}</div>
              <div class="stat-label">收藏</div>
            </div>
          </div>
        </div>

        <!-- 相关推荐卡片 -->
        <div class="recommend-card">
          <div class="recommend-header">
            <i class="el-icon-star-on"></i>
            <span>相关推荐</span>
          </div>

          <div class="recommend-list">
            <div class="recommend-item" v-for="item in recommendList" :key="item.id">
              <a :href="'/front/blogDetail?blogId=' + item.id" target="_blank" class="recommend-link">
                <div class="recommend-title">{{ item.title }}</div>
              </a>
              <div class="recommend-stats">
                <span class="stat"><i class="el-icon-view"></i>{{ item.readCount }}</span>
                <span class="stat"><i class="el-icon-thumb"></i>{{ item.likesCount }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 广告卡片 -->
        <div class="ad-card">
          <div class="ad-content">
            <div class="ad-text">
              <div>找对属于你的学习圈子</div>
              <div>扫码学习精品项目</div>
            </div>
            <img src="@/assets/imgs/广告.png" alt="" class="ad-image">
          </div>
        </div>
      </div>



    </div>

  </div>
</template>

<script>
import Comment from "@/components/Comment";
export default {
  name: "BlogDetail",
  components: {
    Comment
  },
  data() {
    return {
      blogId: this.$route.query.blogId,
      blog: {},
      tagsArr: [],
      recommendList: [],
      commentCount: 0,
      commentContent: '',
      commentList: []
    }
  },
  created() {
    this.load()
    this.$request.put('/blog/updateReadCount/'+this.blogId)
    this.loadComment()
  },
  methods: {
    loadComment() {
      this.$request.get('/comment/selectForUser', {
        params: {  fid: this.blogId, module: '博客' }
      }).then(res => {
        this.commentList = res.data || []
      })
    },
    addComment() {
      this.$request.post('/comment/add', { content: this.commentContent, fid: this.blogId, module: '博客' }).then(res => {
        if (res.code === '200') {
          this.$message.success('操作成功')
          this.commentContent = ''
          this.loadComment()  // 重新加载数据
        }
      })
    },
    setLikes() {
      this.$request.post('/likes/set', {  fid: this.blogId, module: '博客' }).then(res => {
        if (res.code === '200') {
          this.$message.success('操作成功')

          this.load()  // 重新加载数据
        }
      })
    },
    setCollect() {
      this.$request.post('/collect/set', {  fid: this.blogId, module: '博客' }).then(res => {
        if (res.code === '200') {
          this.$message.success('操作成功')

          this.load()  // 重新加载数据
        }
      })
    },
    load() {
      this.$request.get('/blog/selectById/' + this.blogId).then(res => {
        this.blog = res.data || {}

        // this.blog.readCount += 1;
        // this.$request.put('/blog/update',this.blog);

        this.tagsArr = JSON.parse(this.blog.tags || '[]')
      })

      this.$request.get('/blog/selectRecommend/' + this.blogId).then(res => {
        this.recommendList = res.data || []
      })
    }
  }
}
</script>

<style scoped>
.blog-detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  min-height: calc(100vh - 200px);
}

.blog-detail-wrapper {
  display: flex;
  gap: 24px;
  align-items: flex-start;
}

.blog-main {
  flex: 1;
  min-width: 0;
}

.blog-sidebar {
  width: 320px;
  position: sticky;
  top: 100px;
}

/* 博客内容卡片 */
.blog-content-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.blog-content-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.blog-header {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 2px solid rgba(102, 126, 234, 0.1);
}

.blog-title {
  font-size: 2.2rem;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 20px 0;
  line-height: 1.3;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.blog-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  color: #7f8c8d;
  font-size: 14px;
  font-weight: 500;
}

.meta-item i {
  margin-right: 6px;
  color: #667eea;
  font-size: 16px;
}

.blog-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.blog-tag {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%) !important;
  color: #667eea !important;
  border: none !important;
  border-radius: 20px !important;
  padding: 6px 16px !important;
  font-weight: 500 !important;
  transition: all 0.3s ease !important;
}

.blog-tag:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.blog-content {
  line-height: 1.8;
  font-size: 16px;
  color: #2c3e50;
}

/* 互动操作卡片 */
.interaction-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  display: flex;
  justify-content: center;
  gap: 40px;
}

.interaction-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  padding: 16px 24px;
  border-radius: 12px;
  transition: all 0.3s ease;
  color: #7f8c8d;
  background: rgba(102, 126, 234, 0.05);
}

.interaction-item:hover {
  transform: translateY(-2px);
  background: rgba(102, 126, 234, 0.1);
  color: #667eea;
}

.interaction-item.active {
  color: #ff6b6b !important;
  background: rgba(255, 107, 107, 0.1) !important;
}

.interaction-item i {
  font-size: 24px;
  margin-bottom: 8px;
}

.interaction-item span {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 4px;
}

.interaction-label {
  font-size: 12px;
  opacity: 0.8;
}

/* 评论区域 */
.comment-section {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

/* 作者信息卡片 */
.author-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.author-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.author-info {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}

.author-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 3px solid rgba(102, 126, 234, 0.2);
  transition: all 0.3s ease;
}

.author-avatar:hover {
  transform: scale(1.05);
  border-color: #667eea;
}

.author-details {
  flex: 1;
}

.author-name {
  font-weight: 600;
  font-size: 16px;
  color: #2c3e50;
  margin-bottom: 4px;
}

.author-bio {
  color: #7f8c8d;
  font-size: 13px;
  line-height: 1.4;
}

.author-stats {
  display: flex;
  justify-content: space-around;
  padding-top: 16px;
  border-top: 1px solid rgba(102, 126, 234, 0.1);
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 18px;
  font-weight: 600;
  color: #667eea;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #7f8c8d;
}

/* 推荐卡片 */
.recommend-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.recommend-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid rgba(102, 126, 234, 0.1);
}

.recommend-header i {
  color: #667eea;
  margin-right: 8px;
  font-size: 18px;
}

.recommend-header span {
  font-weight: 600;
  color: #2c3e50;
  font-size: 16px;
}

.recommend-item {
  margin-bottom: 16px;
  padding: 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.recommend-item:hover {
  background: rgba(102, 126, 234, 0.05);
}

.recommend-link {
  text-decoration: none;
}

.recommend-title {
  color: #2c3e50;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
  line-height: 1.4;
  transition: all 0.3s ease;
}

.recommend-title:hover {
  color: #667eea;
}

.recommend-stats {
  display: flex;
  gap: 16px;
}

.recommend-stats .stat {
  display: flex;
  align-items: center;
  color: #7f8c8d;
  font-size: 12px;
}

.recommend-stats .stat i {
  margin-right: 4px;
  font-size: 12px;
}

/* 广告卡片 */
.ad-card {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.ad-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.ad-text {
  flex: 1;
  line-height: 1.4;
  font-size: 13px;
  color: #667eea;
  font-weight: 500;
}

.ad-image {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.5);
}

/* 富文本编辑器样式 */
.w-e-text {
  line-height: 1.8;
}

.w-e-text p {
  line-height: 1.8;
  margin-bottom: 16px;
}

.w-e-text ul, .w-e-text ol {
  margin: 16px 0 16px 24px;
}

.w-e-text blockquote {
  display: block;
  border-left: 4px solid #667eea;
  padding: 16px 20px;
  margin: 20px 0;
  line-height: 1.6;
  background: rgba(102, 126, 234, 0.05);
  border-radius: 0 8px 8px 0;
}

.w-e-text code {
  display: inline-block;
  background: rgba(102, 126, 234, 0.1);
  border-radius: 4px;
  padding: 2px 6px;
  margin: 0 2px;
  font-family: 'Courier New', monospace;
  color: #667eea;
}

.w-e-text pre {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  overflow-x: auto;
  margin: 16px 0;
}

.w-e-text pre code {
  display: block;
  background: none;
  padding: 0;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .blog-detail-wrapper {
    flex-direction: column;
  }
  
  .blog-sidebar {
    width: 100%;
    position: static;
  }
  
  .blog-content-card {
    padding: 24px;
  }
  
  .blog-title {
    font-size: 1.8rem;
  }
  
  .interaction-card {
    gap: 20px;
  }
}
</style>